/*
 * ActionForm utilizzata nel caricamento dei file, ovunque sia necessario nel progetto.
 */

package system;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author ciummo
 */
public class FileActionForm extends org.apache.struts.action.ActionForm {
    
        
        private String prop;
	private FormFile file;
 
	public FormFile getFile() {
		return file;
	}
 
	public void setFile(FormFile file) {
		this.file = file;
	}

        public String getProp() {
            return prop;
        }

        public void setProp(String prop) {
            this.prop = prop;
        }

        
        
        
    /**
     *
     */
    public FileActionForm() {
        super();
        // TODO Auto-generated constructor stub
    }

	@Override
	public ActionErrors validate(ActionMapping mapping,
	   HttpServletRequest request) {
 
	    ActionErrors errors = new ActionErrors();
            
            //La verifica del file in ingresso mi serve solamente nel caso in cui debba leggere!
            if (request.getParameter("method").equals("reader")){

                if( getFile().getFileSize()== 0){
                   errors.add("file.err",
                    new ActionMessage("error.file.required"));
                   return errors;
                }

                //only allow textfile to upload
                if(!"text/xml".equals(getFile().getContentType())){
                    errors.add("err.ext",
                     new ActionMessage("error.file.type"));
                    return errors;
                }

                //file size cant larger than 10kb
                if(getFile().getFileSize() > 200240){ //200kb
                   errors.add("err.size",
                        new ActionMessage("error.file.size", 200240));
                   return errors;
                }
            }
 
	    return errors;
	}
}